/**
 * Copyright ©2012-2018 HopeHaiyang Corporation, All Rights Reserved
 * http://www.wuhanhope.com
 */
package hope.platform.data.web;

import hope.platform.data.domain.ObservedData;
import hope.platform.data.server.ObservedDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * class info
 *
 * @author hujia
 *         Create at 2018/7/6 15:56
 */
@RestController
@RequestMapping("/observed")
@Slf4j
public class ObservedDataController {

    @Autowired
    private ObservedDataService observedDataService;

    @RequestMapping(method = RequestMethod.POST)
    public ObservedData addObservedData(@RequestBody ObservedData observedData) throws Exception {
        return observedDataService.addObservedData(observedData);
    }

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public List<ObservedData> addListObservedData(@RequestBody List<ObservedData> dataList) throws Exception {
        List<ObservedData> result = new ArrayList<>();
        for (ObservedData tmp : dataList) {
            result.add(observedDataService.addObservedData(tmp));
        }
        return result;
    }

    @RequestMapping(value = "/{serialNumber}/{dataName}/{beginTime}/{endTime}", method = RequestMethod.GET)
    public List<ObservedData> getObservedDataBySerialNumberAndDataName(@PathVariable String serialNumber,
                                                                       @PathVariable String dataName,
                                                                       @PathVariable String beginTime,
                                                                       @PathVariable String endTime) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date beginDate = sdf.parse(beginTime);
        Date endDate = sdf.parse(endTime);
        return observedDataService.getObservedDataBySerialNumberAndDataNameBetweenDataTime(serialNumber, dataName,
                beginDate, endDate);

    }

    @GetMapping("/dataNames")
    public Map<String, List<ObservedData>> getObservedDataByMap(@RequestParam("names") List<String> dataNames,
                                       @RequestParam("serialNumber") String serialNumber,
                                       @RequestParam("beginTime") String beginTime,
                                       @RequestParam("endTime") String endTime) throws Exception {

        Map<String, List<ObservedData>> results = new HashMap<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (String dataName : dataNames) {
            List<ObservedData> result = observedDataService.getObservedDataBySerialNumberAndDataNameBetweenDataTime(
                    serialNumber, dataName, sdf.parse(beginTime), sdf.parse(endTime));
            results.put(dataName, result);
        }
        return results;
    }


    @RequestMapping(value = "/count/{serialNumber}/{dataName}/{beginTime}/{endTime}", method = RequestMethod.GET)
    public long countObservedDataBySerialNumberAndDataName(@PathVariable String serialNumber,
                                                           @PathVariable String dataName,
                                                           @PathVariable String beginTime,
                                                           @PathVariable String endTime) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date beginDate = sdf.parse(beginTime);
        Date endDate = sdf.parse(endTime);
        return observedDataService.countObservedDataBySerialNumberAndDataNameBetweenDataTime(serialNumber, dataName,
                beginDate, endDate);

    }

    @RequestMapping(value = "/count/{serialNumber}/{beginTime}/{endTime}", method = RequestMethod.GET)
    public long countObservedDataBySerialNumber(@PathVariable String serialNumber,
                                                @PathVariable String beginTime,
                                                @PathVariable String endTime) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date beginDate = sdf.parse(beginTime);
        Date endDate = sdf.parse(endTime);
        return observedDataService.countBySerialNumberAndDataTimeBetween(serialNumber,beginDate, endDate);

    }

    @RequestMapping(value = "/time/{beginTime}/{endTime}", method = RequestMethod.GET)
    public List<ObservedData> getObservedDataByTime(@PathVariable String beginTime,
                                                    @PathVariable String endTime) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date beginDate = sdf.parse(beginTime);
        Date endDate = sdf.parse(endTime);
        return observedDataService.getObservedDataBetweenDataTime(beginDate, endDate);
    }


}
